<?php
namespace Home\Controller;
use Think\Controller;
class CommonController extends Controller 
{
	/**
    *自动执行    简单登陆权限  验证
    */
    public function _initialize()
    {
        //前台网站禁止检测判断
        $site_on_off = site_on_off();
        if ($site_on_off ==0) {
            echo '网站维护中...';
            exit;
        }
        
        $path = CONTROLLER_NAME.'/'.ACTION_NAME;
        if (in_array($path, C('AUTH_PATH'))) {
            //判断登陆否
            if (!$_SESSION['username']) {

                $this->error('非法访问 跳转中...',U('home/login/index'));
            }
            //浏览记录 记录入库
            $this->history(); 
        }else {
            //浏览记录 记录入库
            $this->history(); 
        }
    }

    /**
    *纪录所有历史浏览记录
    *$url [REQUEST_URI] => /fly/index.php/admin/level/index.html 
    *$ip = get_client_ip();
    *
    */
    public function history()
    {
      $data['username'] = $_SESSION['username']?$_SESSION['username']:'游客';
      $data['client_ip'] = get_client_ip();
      $data['url'] = $_SERVER['REQUEST_URI'];
      $data['access_time'] = time();
      //入库
      M('history')->add($data);
    }


    //登陆信息显示
    public function _userinfo($id)
    {
        //$id = $_SESSION['id'];
        $userinfo = M('user')->where(array('id'=>$id))->find();
        //print_r($userinfo);
        if ($userinfo) {
            $this->assign('userinfo',$userinfo);

            //加入时间对比显示
            $time = run($userinfo['create_time']);
            $this->assign('how_time',$time);
        }
    }

    //公共方法 cate栏目分组
    public function catetree($arr,$id=0)
    {
       $tree = array();
       foreach ($arr as $v) {
            if ($v['fid']==$id) {
                $v['son'] = $this->catetree($arr,$v['id']);
                $tree[] = $v;

            }
       }
       return $tree;
    }

    /**
    *自动把字段 加多少 
    *表名$table 字段$field key $id  加多少$num
    */
    public function auto_num($table,$field,$id,$num)
    {
        $sql = 'update '.$table.' set '.$field.'='.$field.'+'.$num. ' where id='.$id;
        $row = M()->execute($sql);
        return $row;

    }





}